﻿using NewsReader.Helpers;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media.Animation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace NewsReaderApp.Views
{
    public sealed partial class PortraitView : UserControl
    {
        public static readonly DependencyProperty IsPopupOpenProperty = DependencyProperty.Register("IsPopupOpen", typeof(bool), typeof(PortraitView), new PropertyMetadata(false, HandleIsPopupOpen));
        private FrameworkElement _lastClicked;
        private readonly Storyboard _openPopupStory;
        private readonly Storyboard _closePopupStory;

        private bool _keepPopupOpen;

        private static void HandleIsPopupOpen(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PortraitView pv = d as PortraitView;
            if (pv == null)
                return;

            if (!(bool)e.NewValue)
            {
                pv.ClosePopup();
            }
        }

        public PortraitView()
        {
            this.InitializeComponent();

            Window.Current.SizeChanged += DisplayProperties_OrientationChanged;
            contentViewHolder.SelectionChanged += contentViewHolder_SelectionChanged;
            this.Tapped += PortraitView_Tapped;

            _openPopupStory = (Storyboard) this.Resources["showSelector"];
            _closePopupStory = (Storyboard)this.Resources["hideSelector"];

        }

        void PortraitView_Tapped(object sender, TappedRoutedEventArgs e)
        {
            if (_keepPopupOpen)
            {
                _keepPopupOpen = false;
            }
            else
            {
                ClosePopup();
            }
        }

        private void contentViewHolder_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var fv = sender as CustomFlipView;
            fv.ActionSelectedItemView(dp =>
                {
                    var view = dp.GetFirstDescendantOfType<ScrollViewer>();
                    if (view != null)
                    {
                        view.ScrollToVerticalOffset(0);
                    }
                });
        }


        private void DisplayProperties_OrientationChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            ClosePopup();
        }

        internal void ClosePopup()
        {
            _closePopupStory.Begin();
        }

        private void ShowFeedsOnPopup()
        {
            feedItemList.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            feedList.Visibility = Windows.UI.Xaml.Visibility.Visible;
            _keepPopupOpen = true;
        }

        private void ShowFeedItemsOnPopup()
        {
            feedItemList.Visibility = Windows.UI.Xaml.Visibility.Visible;
            feedList.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            _keepPopupOpen = true;
        }

        private void HandleSelectorClick(object sender, RoutedEventArgs e)
        {
            var source = (FrameworkElement)sender;
            var gt = source.TransformToVisual(this);
            var origin = gt.TransformPoint(new Point(0, source.RenderSize.Height));
            bool reopen = true;

            if (itemSelector.Opacity != 0)
            {
                reopen = _lastClicked != source;
            }

            if (reopen)
            {
                _lastClicked = source;
                Point top = new Point(origin.X + source.Margin.Left, origin.Y + source.Margin.Top);

                if (source == selectedFeedItem)
               {
                    ShowFeedItemsOnPopup();
                }
                else
                {
                    ShowFeedsOnPopup();
                }

                _openPopupStory.Begin();
                //itemSelector.Opacity = 1;
            }
            else
            {
                _closePopupStory.Begin();
                //itemSelector.Opacity = 0;
            }
        }

        public bool IsPopupOpen
        {
            get { return (bool)GetValue(IsPopupOpenProperty); }
            set { SetValue(IsPopupOpenProperty, value); }
        }
    }
}
